/**
 * Context.java 1.0 2007-6-21 下午09:37:14,创建 by yangfan
 */


package org.in4j.compiler.sandbox.calcuator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author yangfan
 * @version 1.0 2007-6-21 下午09:37:14,创建
 */
public class Context
{
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(Context.class);

	private ListIterator<String> tokens;
	private String currentToken;

	public Context(String text)
	{
		StringTokenizer tokenizer = new StringTokenizer(text);
		List<String> tokenList = new ArrayList<String>();
		while(tokenizer.hasMoreTokens())
		{
			tokenList.add(tokenizer.nextToken());
		}
		this.tokens = tokenList.listIterator();
	}

	public void nextToken()
	{
		if(tokens.hasNext())
		{
			currentToken = tokens.next();
			System.out.println("currentToken:" + currentToken);
		}
		else
		{
			currentToken = null;
		}
//		return currentToken;
	}

	public String currentToken()
	{
		return currentToken;
	}

	public void back()
	{
		tokens.previous();
	}

	public boolean hasNextToken()
	{
		return tokens.hasNext();
	}

//	public void skipToken(String token)
//	{
//		if(!token.equals(currentToken))
//		{
//			throw new RuntimeException("Warning: " + token + " is expected, but " + currentToken + " is found.");
//		}
//		nextToken();
//	}

	public int currentNumber()
	{
		int number = 0;
		try
		{
			number = Integer.parseInt(currentToken);
		}
		catch(NumberFormatException e)
		{
			throw new RuntimeException("Warning: " + e);
		}
		return number;
	}
}
